var URL = getInitUrls();

// 当前js的success的html的js
function AjaxsearchDefSearch(rs) {
	$('#history-float').html('');
	$('#xiala-float').html('');
	var html = '';
	var html2 = '';
	var num = 1;
	for (var i in rs.new) {
		if(i >7){
			break;
		}

		if(rs.new[i] != null){
			html += '<a href="javascript:;" onclick="on_search_float('+ "'"+ rs.new[i].name + "'" +',this,1)">'+ rs.new[i].name + '</a>';
			$('#history-float').html(html);
			num++;
		}
	}
	var l = 1;
	for (var j in rs.newTop) {
		if (l <= 3) {
			html2 += '<li class="z_spanName_float">';
			html2 +=    '<a id="u_float'+num+'" onclick="on_search_float('+ "'"+rs.newTop[j].name + "'" +',this)" href="javascript:;" class="every-record">';
			html2 +=       '<span class="record-name">' + rs.newTop[j].name + '</span>';
			html2 +=       '<span class="record-number-wrap"><span class="record-number">' + rs.newTop[j].count + '</span>结果</span>';
			html2 +=    '</a>';
			html2 +='</li>';
			num++;
		} else {
			html2 += '<li class="z_spanName_float">';
			html2 +=    '<a id="u_float'+num+'" onclick="on_search_float('+ "'"+rs.newTop[j].name + "'" +',this)" href="javascript:;" class="every-record">';
			html2 +=       '<span class="record-name">' + rs.newTop[j].name + '</span>';
			html2 +=       '<span class="record-number-wrap"><span class="record-number">' + rs.newTop[j].count + '</span>结果</span>';
			html2 +=    '</a>';  
			html2 +='</li>';
		}
		num++;
		l++
		$('#xiala-float').html(html2);
	}
}
// home.js的success的html的js
function AjaxsearchDefHome(rs) {
	$('#history').html('');
	$('#xiala').html('');
	var html = '<span>搜索历史 :</span>';
	var html2 = '';
	var num = 1;
	if (rs.new[0] == null) {
		$('#history').html('<span>暂无搜索记录</span>');
	} else {
		for (var i in rs.new) {
			if(rs.new[i] != null){
				html += '<a href="javascript:;" onclick="on_search('+ "'"+ rs.new[i].name + "'" +',this,1)">'+ rs.new[i].name + '</a>';
				$('#history').html(html);
				num++;
			}
		}
	}
	var l = 1;
	for (var j in rs.newTop) {
		if (l <= 3) {
			html2 += '<li class="z_spanName">';
			html2 +=    '<a id="u_'+num+'" onclick="on_search('+ "'"+rs.newTop[j].name + "'" +',this)" href="javascript:;" class="every-record">';
			html2 +=       '<span class="record-name">' + rs.newTop[j].name + '</span>';
			html2 +=       '<span class="record-number-wrap"><span class="record-number">' + rs.newTop[j].count + '</span>结果</span>';
			html2 +=    '</a>';
			html2 +='</li>';
			num++;
		} else {
			html2 += '<li class="z_spanName">';
			html2 +=    '<a id="u_'+num+'" onclick="on_search('+ "'"+rs.newTop[j].name + "'" +',this)" href="javascript:;" class="every-record">';
			html2 +=       '<span class="record-name">' + rs.newTop[j].name + '</span>';
			html2 +=       '<span class="record-number-wrap"><span class="record-number">' + rs.newTop[j].count + '</span>结果</span>';
			html2 +=    '</a>';
			html2 +='</li>';
		}
		num++;
		l++
		$('#xiala').html(html2);
	}
}
// public.js的success的html的js
function AjaxsearchDefPublic(rs) {
	$('#all_history').html('');
	$('#all_xiala').html('');
	var html = '';
	var num = 1;
	if(rs.new[0] != null){
		$("#all_history").attr("onclick","on_search('"+rs.new[0].name+"',this,1)");
		$('#all_history').html(rs.new[0].name);
		num++;
	}
	var l = 1;
	for (var j in rs.newTop) {
		//固定5个，以免影响布局
		if(j > 4){
			break;
		}
		if (l <= 3) {
			html += '<li class="z_spanName1">';
			html +=    '<a id="u1_'+num+'" onclick="on_search('+ "'"+rs.newTop[j].name + "'" +',this)" href="javascript:;">'+rs.newTop[j].name+'</a>';
			html +='</li>';
			num++;
		} else {
			html += '<li class="z_spanName1">';
			html +=    '<a id="u1_'+num+'" onclick="on_search('+ "'"+rs.newTop[j].name + "'" +',this)" href="javascript:;">'+rs.newTop[j].name+'</a>';
			html +='</li>';
		}
		num++;
		l++
		$('#all_xiala').html(html);
	}	
}
// public.js的success的html的js
function AjaxsearchDefHeaderSearch(rs) {
	$('#history-header').html('');
	$('#xiala-header').html('');
	var html = '';
	var html2 = '';
	var num = 1;
	for (var i in rs.new) {
		if(i >7){
			break;
		}

		if(rs.new[i] != null){
			html += '<a href="javascript:;" onclick="on_search_header('+ "'"+ rs.new[i].name + "'" +',this,1)">'+ rs.new[i].name + '</a>';
			$('#history-header').html(html);
			num++;
		}
	}
	var l = 1;
	for (var j in rs.newTop) {
		if (l <= 3) {
			html2 += '<li class="z_spanName_header">';
			html2 +=    '<a id="u_header'+num+'" onclick="on_search_header('+ "'"+rs.newTop[j].name + "'" +',this)" href="javascript:;" class="every-record">';
			html2 +=       '<span class="record-name">' + rs.newTop[j].name + '</span>';
			html2 +=       '<span class="record-number-wrap"><span class="record-number">' + rs.newTop[j].count + '</span>结果</span>';
			html2 +=    '</a>';
			html2 +='</li>';
			num++;
		} else {
			html2 += '<li class="z_spanName_header">';
			html2 +=    '<a id="u_header'+num+'" onclick="on_search_header('+ "'"+rs.newTop[j].name + "'" +',this)" href="javascript:;" class="every-record">';
			html2 +=       '<span class="record-name">' + rs.newTop[j].name + '</span>';
			html2 +=       '<span class="record-number-wrap"><span class="record-number">' + rs.newTop[j].count + '</span>结果</span>';
			html2 +=    '</a>';
			html2 +='</li>';
		}
		num++;
		l++
		$('#xiala-header').html(html2);
	}
}
//搜索预加载
$(document).ready(function(){
    var defUrl = '/index.php?m=Ajaxsearch&a=def';
    if (typeof(thisIsATest) != "undefined" ) {
        defUrl += ('&type='+thisIsATest);
    }
    $.ajax({
        type: 'POST',
        url: defUrl,
        data: {id: 1},
        dataType: 'json',
        success: function (rs) {
			AjaxsearchDefSearch(rs);
			AjaxsearchDefHome(rs);
			AjaxsearchDefPublic(rs);
			AjaxsearchDefHeaderSearch(rs);
        }
    });
});

/**
 * 下拉点击搜索
 */
function on_search_float(keyword,e){
    $('.home .search-tips-bannner').hide();
    $('#head-classify-inp').val(keyword)
    var sort =  $(e).attr('id')
    $('#sort').val(sort)
    search_float(keyword)
}


/**
 * 搜索JS
 */
var formSearch_float = function(){
    var kw = $('#head-classify-inp').val();
    if(kw == '' || kw == '搜索素材'){
        alert('请填写关键词');
        return false;
    }
    search_float(kw);
	location.href='/plus/search.php?q='+kw;
    //$.getJSON(URL.AJAX+"/?m=clickstats&lx=500&callback=?", function(e) {});
}


$('.head-search-wrap').on('click', formSearch_float);


$('#head-classify-inp').keydown(function(event){if(event.which == 13){formSearch_float();}});


function search_float(kw){
    $.get("/index.php?m=search&a=check", {kw:kw}, function(data) {
        if(data.status == 1){
            tj_keyword()
            setTimeout(function(){
                if(data.rewrite){
                    window.location.href="/index.php?m=tags&a=index&class=0-0-0-0-0-1&key=" + data.py;
                }else{
                    window.location.href=URL.DOMAIN+"/tupian/"+data.py+".html";
                }
            },500);
        }else{
            alert('请填写有效的关键词');
        }
    }, 'json');
}



/**
 * 搜索
 */
$("#head-classify-inp").keyup(function(H){
    if (window.event) {
        var G = window.event.keyCode;
    } else {
        var G = H.which;
    }
    if (G != 38 && G != 40 && G != 13) {
        var num = 1;
        D = 0;
        if($('#head-classify-inp').val() == ''){
            //默认搜索词
        }else{
            $('#sort').remove()
            $.ajax({
                type: 'POST',
                url: '/index.php?m=Ajaxsearch&a=index',
                data: {keyword: $('#head-classify-inp').val()},
                dataType: 'json',
                success: function (rs) {
                    $('#history-float').html('');
                    $('#xiala-float').html('');
                    var html = '';
                    for (var i in rs) {
                        html += '<li class="z_spanName_float">';
                        html +=    '<a id="u_float'+num+'" onclick="on_search('+ "'"+rs[i].keyword + "'" +',this)" href="javascript:;" class="every-record">';
                        html +=       '<span class="record-name">' + rs[i].keyword + '</span>';
                        html +=       '<span class="record-number-wrap"><span class="record-number">' + rs[i].icount + '</span>结果</span>';
                        html +=    '</a>';
                        html += '</li>';
                        $('#xiala-float').html(html);
                        num++
                    }

                    if(num <= 1){
                        $('.float-search-tips').hide();
                    }else{
                        $('.float-search-tips').show();
                    }
                }
            });
        }
    } else {
        topkey_float(H)
    }
});

/**
 * 下键选中
 */
var D = 0;
function topkey_float(H) {
    if (window.event) {
        var G = window.event.keyCode;
    } else {
        var G = H.which;
    }
    if (G != 38 && G != 40 && G != 13) {
        D = 0;
    } else {
        if ($(".float-search-tips").css("display") == "block") {
            var I = $(".z_spanName_float").length;
            if (G == 38) {
                D--;
                if (D < 1) {
                    D = I;
                    C_float(D);
                    F_float(1);
                } else {
                    F_float(D + 1);
                    C_float(D);
                }
                return false;
            }
            if (G == 40) {
                D++;
                if (D > I) {
                    D = 1;
                    C_float(1);
                    F_float(I);
                } else {
                    F_float(D - 1);
                    C_float(D);
                }
                return false;
            }
        } else {
            D = 0;
        }
    }
}
function C_float(G) {
    $("#u_float" + G).parent().css("background-color", "#EBEBEB");
    var name = $("#u_float" + G + " .record-name").html()
    $('#head-classify-inp').val(name)
}
function F_float(G) {
    $("#u_float" + G).parent().css("background-color", "#fff");
}

function search_float(kw){
    $.get(URL.DOMAIN+"/index.php?m=search&a=check", {kw:kw}, function(data) {
        if(data.status == 1){
            tj_keyword()
            setTimeout(function(){
                if(data.rewrite){
                    window.location.href=URL.DOMAIN+"/index.php?m=tags&a=index&class=0-0-0-0-0-1&key=" + data.py;
                }else{
                    window.location.href=URL.DOMAIN+"/tupian/"+data.py+".html";
                }
            },500);
        }else{
            alert('请填写有效的关键词');
        }
    }, 'json');
}

/******************************************** 搜索JS ********************************************/
    //有用 不要删
     $(document).ready(function(){
         var c1 = $('#class-c1').val();
         var c2 = $('#class-c2').val();
         var c3 = $('#class-c3').val();
         var c4 = $('#class-c4').val();
         //绑定搜索按钮(按分类)
         $('#condition-search-btn').on('click', formSearch_C);
         function formSearch_C(){
             var kw = $.trim($('#condition-so-kw').val());
             if(kw == '' || kw == '在当前条件下搜索'){
                 alert('请填写关键词');
                 return false;
             }
             search_C(kw);
			 location.href='/plus/search.php?q='+kw;
             //$.getJSON(URL.AJAX+"/?m=clickstats&lx=500&callback=?", function(e) {});
         }


         $('#condition-so-kw').keydown(function(event){if(event.which == 13){formSearch_C();}});

         //各分类需要重写改方法
         function search_C(kw){
             $.get("/index.php?m=search&a=check", {kw:kw}, function(data) {
                 if(data.status == 1){
                     setTimeout(function(){
                         if(data.rewrite){
                             window.location.href="/index.php?m=tags&a=index&class=0-0-0-0-0-1&key=" + data.py;
                         }else{
                             window.location.href="/tupian/"+data.py+"/"+c1+"-"+c1+"-"+c2+"-"+c3+"-"+c4+"-0-c0-1.html";
                         }
                     },500);
                 }else{
                     alert('请填写有效的关键词');
                 }
             }, 'json');
         }
     });

     /******************************************** 搜索JS ********************************************/